import { getAll, getSect,getEdrmsTypeList } from '@/services';
import { ResponseHlAllData, ResponseHlSectData } from '@/services/types';
import { takeEvery, call, put, select } from 'redux-saga/effects';
import { MAIN } from '../constants';
import { get_edrms_type } from '../actions/main';

export function* mainSaga() {
  yield takeEvery(MAIN.GET_ALL_DATA, function* (): any {
    const res: ResponseHlAllData = yield call(getAll);
    if (res.result) {
      yield put({
        type: MAIN.SET_ALL_DATA,
        payload: {
          allData: res.obj,
        },
      });
    }
  });

  yield takeEvery(MAIN.GET_SECT_DATA, function* (): any {
    const curTab = yield select((state) => state.main.curTab);
    const res: ResponseHlSectData = yield call(getSect, curTab);
    if (res.result) {
      yield put({
        type: MAIN.SET_SECT_DATA,
        payload: {
          sectData: {
            sectName: curTab + 'Data',
            value: res.obj,
          },
        },
      });
    }
  });

  yield takeEvery(MAIN.GET_EDRM_TYPE, function* (): any {

    const res: ResponseHlSectData = yield call(getEdrmsTypeList);

    if (res) {
      yield put({
        type: MAIN.GET_EDRM_TYPE,
        payload: {
          edrmsTypeDto: res
        },
      });
    }
  });
}
